package cn.com.blueInfo.core.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.format.DateTimeFormatter;

/**
 * 在vue内嵌时且路由模式为history模式时启用该配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${server.vue-config}")
    private boolean vueRouteConfig;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        if (vueRouteConfig) {
            registry.addViewController("/{path:[^\\.]*}")
                    .setViewName("forward:/index.html");  // 非静态资源请求转发到index.html
        }
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 配置 LocalDate 转换器
        DateTimeFormatterRegistrar dateRegistrar = new DateTimeFormatterRegistrar();
        dateRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        dateRegistrar.registerFormatters(registry);

        // 配置 LocalDateTime 转换器
        DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
        dateTimeRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        dateTimeRegistrar.registerFormatters(registry);
    }
}
